package com.jhws.business.comm.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;

import com.jhws.business.comm.bo.TblCommDimBo;
import com.jhws.common.Page;

@Transactional
public class TblCommDimHibernateDAO implements
		ITblCommDimDAO {

	private SessionFactory sessionFactory;


	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public TblCommDimBo getById(String strId) {
		return (TblCommDimBo) sessionFactory.getCurrentSession().get("com.jhws.business.comm.bo.TblCommDimBo",
				strId);
	}

	public Page findTblCommDim(int start, int num, String whereClause)
			throws Exception {
		try {
			StringBuffer buf = new StringBuffer();
			buf.append("from TblCommDimBo ");
			buf.append(whereClause);
			buf.append("order by comm_code");

			Query query = sessionFactory.getCurrentSession().createQuery(buf.toString());
			query.setFirstResult(start);
			query.setMaxResults(num);
			List list = query.list();

			Page page = new Page(start, num, list);
			String hql = "select count(*) from TblCommDimBo "+ (whereClause == null || "".equals(whereClause.trim()) ? "" : whereClause.trim());
			query = sessionFactory.getCurrentSession().createQuery(hql);
			page.setTotalNumber(((Number)query.uniqueResult()).intValue());
			return page;
		} catch (Exception e) {
			System.out.println("======================e:"+e.getMessage());
			return new Page();
		}
	}

	public TblCommDimBo save(TblCommDimBo bo) {
		sessionFactory.getCurrentSession().saveOrUpdate(bo);
		return bo;
	}

	public TblCommDimBo merge(TblCommDimBo bo) {
		sessionFactory.getCurrentSession().merge(bo);
		return bo;
	}

	public List<TblCommDimBo> findTblCommDimByWhere(String whereClause) throws Exception {
		try {
			StringBuffer buf = new StringBuffer("from TblCommDimBo ");
			if(StringUtils.isNotEmpty(whereClause))
			{
				buf.append(whereClause);
			}
			buf.append(" order by comm_code");
			Query query = sessionFactory.getCurrentSession().createQuery(buf.toString());

			@SuppressWarnings("unchecked")
			List<TblCommDimBo> list = query.list();
			return list;
		} catch (Exception e) {
			return new ArrayList<TblCommDimBo>();
		}
	}

	public void deleteTblCommDim(String TblCommId) {

		this.sessionFactory.getCurrentSession().delete(getById(TblCommId));
//		this.getSession().createQuery("delete from TblCommDimBo where comm_code = ?")
//				.setString(0, TblCommId).executeUpdate();
	}


	public TblCommDimBo getTblComm(TblCommDimBo bo) {
		Query q = sessionFactory.getCurrentSession().createQuery("from TblCommDimBo where comm_Name = ?");
		q.setParameter(0, new Object[]{bo.getCommName()});
		List list = q.list();
		if(list.size() != 0)
			return (TblCommDimBo) list.get(0);
		else
			return null;
	}

}	
